package org.xqp.user.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xqp.basic.exception.MyException;
import org.xqp.basic.util.JsonResult;
import org.xqp.basic.util.PageList;
import org.xqp.user.domain.User;
import org.xqp.user.dto.UserDto;
import org.xqp.user.query.UserQuery;
import org.xqp.user.service.IUserService;

import java.util.List;

/**
 * 用户操作
 */
@RestController // @Controller @ResponseBody
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     *  url: localhost:8080/user/query?id=1  以前的方式
     *  url: localhost:8080/user/1  restful的方式
     * @param id
     * @return
     */
    // @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据",notes = "传一个id")
    public User queryById(@PathVariable("id") Long id){
        return userService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "不需要参数")
    public List<User> queryAll(){
        return userService.findAll();
    }

    /**
     * @param query
     * @return
     *  {
     *      currentPage: 1,
     *      pageSize: 10,
     *      ....
     *  }
     *  查询分页数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "分页。。。")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryList(query);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "传一个id")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙。。");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "传id数组")
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            userService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙。。");
        }
    }

    /**
     * 添加和修改操作
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public JsonResult addAndUpdate(@RequestBody User user){
        try {
            if(user.getId()==null){ // 添加操作
                userService.insert(user);
            }else{ // 修改操作
                userService.update(user);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙");
        }
    }
    @PostMapping("/phone/register")
    public JsonResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return JsonResult.me();
        } catch (MyException e) {
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            return JsonResult.me().setMsg("注册失败");
        }
    }
}
